extends KinematicBody2D
export(PackedScene) var bullet_scene
export(PackedScene) var manafocus_scene

onready var anim_sprite = $Sprite
onready var anim_tree_main = $AnimationTreeMain
onready var anim_player_main = $AnimationTreeMain/AnimationPlayerMain

onready var anim_playback_root = anim_tree_main["parameters/playback"]
onready var anim_playback_main = anim_tree_main["parameters/mainAnimStateMachine/playback"]

var world
var aim
var aimx
var aimy

var motion = Vector2.ZERO
		
var move_speed_slow = 15
var move_speed = 120

var hp = 2000
var hp_max = 2000
var is_pro = false
var is_true_pro = false

var action = 0
var action_cd = 3.0
var action_count = 2
var action_count_pro = 3
var action_count_true_pro = 5
var action_timer = 0.0

var is_spelling = false
var spell_count = 3
var current_spell = 0
var spell_timer = 0
var spell_self_timer = 0
var is_spell_ready = false
var is_aim_checked = false
var spell_angel = 0.0

var x_flip = -1
var x_flip_lock = false

var hit_delay = 0
var hit_delay_max = 0.7
var anim_hit_frame = 0
var anim_hit_frame_max = 3

var hp_bar

func set_hp_bar(node):
	hp_bar = node
	hp_bar.min_value = 0
	hp_bar.max_value = hp_max
	hp_bar.value = hp

func set_world(node):
	world = node

func set_aim(node):
	aim = node

func idle(delta):
	aimx = (-position.x + aim.position.x)
	aimy = (position.y - aim.position.y - 12)
	motion = Vector2(aimx,aimy).normalized()
	
	motion.x *= move_speed_slow
	motion.y *= move_speed_slow
	
	move_and_slide(motion)
	
# Maga Fusillade
func spell0(delta):
	# Aim Check
	if is_aim_checked == false:
		aimx = (-position.x + aim.position.x)
		aimy = (-position.y + aim.position.y)
		spell_timer = 3.8
		is_aim_checked = true
		
	# Move to spell position
	if is_spell_ready == false:
#		set_collision_mask_bit(0,false)
		motion = Vector2(
			aimx,
			aimy
			).normalized()
		
		motion.x *= move_speed
		motion.y *= move_speed
		
		move_and_slide(motion)
		if spell_timer < 3.5:
			is_spell_ready = true
			anim_playback_main.travel("spell0")
			spell_self_timer = 0
			
	# Spell
	else:
#		set_collision_mask_bit(0,true)
		if spell_timer >= 2:
			if spell_self_timer <= 0:
				var manafocus = manafocus_scene.instance()
				manafocus.position.x = position.x + x_flip * 12
				manafocus.position.y = position.y - 12
				manafocus.rotation_degrees = randi() % 360
				world.add_child(manafocus)
				
				spell_self_timer = 0.2
				pass
			else:
				print("spell focus countdown")
				spell_self_timer -= delta
				
		if spell_timer < 3:
			# Bullet Shoot
			var bullet_amount = 1
			if spell_self_timer <= 0:
				if is_true_pro == true:
					bullet_amount = 3
				for i in range(bullet_amount):
					
					var bullet = bullet_scene.instance()
					var bullet_direct = cartesian2polar(
						(-position.x - x_flip * 12 + aim.position.x),
						(-position.y + aim.position.y)
						).y
					print(bullet_direct)
					bullet_direct += (i + 1 - (bullet_amount + 1.0) / 2.0) * PI / 8
					bullet.direct = polar2cartesian(1, bullet_direct)
					bullet.move_speed = 150
					bullet.position.x = position.x + x_flip * 12
					bullet.position.y = position.y - 12
					bullet.attack_damage = 5
					
					world.add_child(bullet)
					spell_self_timer = 0.2
				pass
			else:
				spell_self_timer -= delta
		
# Slapping the Sunshine
func spell1(delta):
	# Aim Check
	if is_aim_checked == false:
		aimx = 0
		aimy = 1
		spell_timer = 3.6
		is_aim_checked = true
		
	# Move to spell position
	if is_spell_ready == false:
#		set_collision_mask_bit(0,false)
		motion = Vector2(
			aimx,
			aimy
			).normalized()
		
		motion.x *= move_speed
		motion.y *= move_speed
		
		move_and_slide(motion)
		if spell_timer < 3:
			anim_playback_main.travel("spell1")
			is_spell_ready = true
			aimx = (-position.x + aim.position.x)
			aimy = (-position.y + aim.position.y)
		
			spell_angel = cartesian2polar(aimx, aimy).y
			# spell_angel = x_flip * PI / 2
	# Spell
	else:
		x_flip_lock = true
#		set_collision_mask_bit(0,true)
		if spell_timer >= 1:
			if spell_self_timer <= 0:
				var manafocus = manafocus_scene.instance()
				manafocus.position.x = position.x
				manafocus.position.y = position.y - 8
				manafocus.rotation_degrees = randi() % 360
				
				world.add_child(manafocus)
				spell_self_timer = 0.1
				pass
			else:
				print("spell focus countdown")
				spell_self_timer -= delta
				
		if spell_timer < 1.4:
			anim_playback_main.travel("spell2")
			# Bullet Shoot
			if spell_self_timer < 0:
				var bullet = bullet_scene.instance()
				bullet.direct = polar2cartesian(1, spell_angel)
				spell_angel += x_flip * PI * 16 / 41
				
				bullet.move_speed = 110
				bullet.position.x = position.x
				bullet.position.y = position.y - 8
				bullet.attack_damage = 5
				
				world.add_child(bullet)
				spell_self_timer = 0.016
				pass
			else:
				spell_self_timer -= delta

# Revolving Flower Snow
func spell2(delta):
	# Aim Check
	if is_aim_checked == false:
		aimx = 160 - position.x
		aimy = 96 - position.y
		spell_timer = 8
		is_aim_checked = true
		
	# Move to spell position
	if is_spell_ready == false:
		set_collision_mask_bit(0,false)
		aimx = 160 - position.x
		aimy = 96 - position.y
		motion = Vector2(
			aimx,
			aimy
			).normalized()
		
		motion.x *= move_speed
		motion.y *= move_speed
		
		if abs(aimx) > 2 or abs(aimy) > 2:
			move_and_slide(motion)
		else:
			anim_playback_main.travel("spell1")
			is_spell_ready = true
			spell_timer = 6
			aimx = (-position.x + aim.position.x)
			aimy = (-position.y + aim.position.y)
		
	# Spell
	else:
		x_flip_lock = true
		set_collision_mask_bit(0,true)
		if spell_timer >= 2.5:
			if spell_self_timer <= 0:
				var manafocus = manafocus_scene.instance()
				manafocus.position.x = position.x
				manafocus.position.y = position.y - 8
				manafocus.rotation_degrees = randi() % 360
				
				world.add_child(manafocus)
				spell_self_timer = 0.1
				pass
			else:
				print("spell focus countdown")
				spell_self_timer -= delta
				
		if spell_timer < 4 and spell_timer > 0.5:
			anim_playback_main.travel("spell2")
			# Bullet Shoot
			if spell_self_timer < 0:
				var bullet = bullet_scene.instance()
				bullet.direct = polar2cartesian(1, spell_angel)
				spell_angel += x_flip * PI * 10 / 51
				
				bullet.move_speed = 110
				bullet.position.x = position.x
				bullet.position.y = position.y - 8
				bullet.attack_damage = 5
				
				world.add_child(bullet)
				spell_self_timer = 0.012
				
				pass
			else:
				spell_self_timer -= delta

# Snow Lotus Blooming Forever
func spell4(delta):
	# Aim Check
	if is_aim_checked == false:
		aimx = 160 - position.x
		aimy = 96 - position.y
		spell_timer = 15
		is_aim_checked = true
		
	# Move to spell position
	if is_spell_ready == false:
		set_collision_mask_bit(0,false)
		aimx = 160 - position.x
		aimy = 96 - position.y
		motion = Vector2(
			aimx,
			aimy
			).normalized()
		
		motion.x *= move_speed
		motion.y *= move_speed
		
		if abs(aimx) > 1 or abs(aimy) > 1:
			move_and_slide(motion)
		else:
			anim_playback_main.travel("spell1")
			is_spell_ready = true
			spell_timer = 7.5
			aimx = (-position.x + aim.position.x)
			aimy = (-position.y + aim.position.y)
		
	# Spell
	else:
		x_flip_lock = true
		set_collision_mask_bit(0,true)
		if spell_timer >= 2.5:
			if spell_self_timer <= 0:
				var manafocus = manafocus_scene.instance()
				manafocus.position.x = position.x
				manafocus.position.y = position.y - 8
				manafocus.rotation_degrees = randi() % 360
				
				world.add_child(manafocus)
				if spell_timer > 6:
					spell_self_timer = 0.2
			else:
				print("spell focus countdown")
				
		if spell_timer < 6 and spell_timer > 0.5:
			anim_playback_main.travel("spell2")
			# Bullet Shoot
			if spell_self_timer < 0:
				for i in range(5):
					var bullet = bullet_scene.instance()
					bullet.direct = polar2cartesian(1, spell_angel + x_flip * PI * i * 7 / 9)					
					bullet.move_speed = 110
					bullet.position.x = position.x
					bullet.position.y = position.y - 8
					bullet.attack_damage = 5
					
					world.add_child(bullet)
				for i in range(3):
					var bullet = bullet_scene.instance()
					bullet.direct = polar2cartesian(1, -spell_angel * 2 - x_flip * PI * i * 1 / 13)					
					bullet.move_speed = 60
					bullet.position.x = position.x
					bullet.position.y = position.y - 8
					bullet.attack_damage = 5
					
					world.add_child(bullet)
				spell_self_timer = 0.2
				spell_angel += x_flip * PI * 7 / 37
			else:
				pass
		spell_self_timer -= delta

func defeat():
	spell_timer = 0
	is_spelling = false
	get_parent().get_node("totalUI/Win").visible = true
	get_parent().get_node("totalUI/Reset").visible = true
	
func take_damage(damage):
	hp -= damage
	hp_bar.value = hp
	if hp <= 0:
		defeat()
	
	if is_spelling == false:
		hit_delay = hit_delay_max
		print("hit switch" + String(anim_hit_frame))
		anim_playback_root.travel("hit" + String(anim_hit_frame))
		anim_hit_frame = (anim_hit_frame + 1) % anim_hit_frame_max

var spellbook = [
	"spell0",
	"spell1",
	"spell2"
]

# Called when the node enters the scene tree for the first time.
func _ready():
	hp = hp_max
	set_hp_bar(get_parent().get_node("totalUI/BossHPBar"))
	
	action_timer = action_cd / 2
	is_pro = false
	action = 0
	set_world(get_parent())
	set_aim(get_parent().find_node("Hero"))

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	
func pro_power():
	action_count = action_count_pro
	is_pro = true
	action = action_count - 1

func true_pro_power():
	action_count = action_count_true_pro
	get_parent().get_node("totalUI/TruePro").visible = true
	spellbook = [
		"spell0",
		"spell1",
		"spell2",
		"spell0",
		"spell4"
	]
	get_parent().get_node("totalUI/TruePro/AnimationPlayer").play("Show")
	is_true_pro = true
	action = action_count - 1
	action_timer = 0.5
func _physics_process(delta):
	# Defeat
	if hp <= 0:
		return
	# Pro
	if is_spelling == false and hp < hp_max / 2 and is_pro == false:
		pro_power()
		# True Pro
		if aim.hp == aim.hp_max:
			true_pro_power()
			
	# Timer
	if hit_delay > 0:
		hit_delay -= delta
	if spell_timer > 0:
		spell_timer -= delta
	if action_timer > 0 and is_spelling == false:
		action_timer -= delta
	
	# Move and Action
	
	if action_timer <= 0:
		current_spell = action
		print("spell"+ String(current_spell))
		is_spelling = true
		hit_delay = 0
		is_spell_ready = false
		is_aim_checked = false
		action_timer = action_cd
		action = (action + 1) % action_count
	elif is_spelling == false:
		print("idle")
		print(action_timer)
		idle(delta)
	
	# Spell
	if is_spelling:
		anim_playback_root.travel("mainAnimStateMachine")
		print("is_spelling")
		call(spellbook[current_spell],delta)
	
	if spell_timer <= 0 and is_spelling:
		x_flip_lock = false
		is_spelling = false
		action_timer = 3
	
	# Animation
	if x_flip_lock == false:
		if (-position.x + aim.position.x) > 0:
			x_flip = 1
		else:
			x_flip = -1
			
		if x_flip == 1:
			anim_sprite.flip_h = false
		elif x_flip == -1:
			anim_sprite.flip_h = true
		
	if hit_delay <= 0 and is_spelling == false:
		anim_playback_root.travel("mainAnimStateMachine")
		anim_playback_main.travel("idle")
	
			
